package com.lhq.lingsi_study_hall.scoring;

import com.lhq.lingsi_study_hall.common.ErrorCode;
import com.lhq.lingsi_study_hall.exception.BusinessException;
import com.lhq.lingsi_study_hall.model.entity.App;
import com.lhq.lingsi_study_hall.model.entity.UserAnswer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评分策略执行器
 */
@Service
public class ScoringStrategyExecutor {
    //策略列表
    @Resource
    private List<ScoringStrategy> scoringStrategyList;


    public UserAnswer doScore(List<String> choices, App app) {
        //根据appType和scoringStrategy获取对应的策略
        Integer appType = app.getAppType();
        Integer scoringStrategy = app.getScoringStrategy();
        if (appType == null || scoringStrategy == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"应用配置有误，未找到配置的策略");
        }
        //根据注解获取策略
        for (ScoringStrategy strategy : scoringStrategyList) {
            boolean annotationPresent = strategy.getClass().isAnnotationPresent(ScoringStrategyConfig.class);
            if (annotationPresent) {
                ScoringStrategyConfig annotation = strategy.getClass().getAnnotation(ScoringStrategyConfig.class);
                if (annotation.appType()==appType && annotation.scoringStrategy()==scoringStrategy) {
                    return strategy.doScore(choices, app);
                }
            }
        }
        throw new BusinessException(ErrorCode.PARAMS_ERROR,"未找到对应的策略");
    }
}
